/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  TextIO
 * 
 * Description of class:
 * 
 * Class that helps with loading and saving txt files.
 * 
 */

package karel.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class TextIO {

    public static String loadFile(File file) throws Exception {
        String res = "";
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                new FileInputStream(file), "UTF8"));
        String s;
        while ((s = in.readLine()) != null) {
            res += s+"\n";
        }

        in.close();
        return res;
    }
    
    public static String loadFile(InputStream input) throws Exception {
        String res = "";
        BufferedReader in = new BufferedReader(
                new InputStreamReader(input, "UTF8"));
        String s;
        while ((s = in.readLine()) != null) {
            res += s+"\n";
        }

        in.close();
        return res;
    }

    public static void saveFile(File file, String string) throws Exception {
        Writer out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(file), "UTF8"));
        out.append(string);
        out.flush();
        out.close();
    }
}
